import 'package:flutter_rfid_dmo/rfid/utils/data_convert.dart';

class RfidDeviceInfo {
  String deviceName = '';
  String macAddress = '';
  String hwVersion = '';
  String swVersion = '';
  String batteryLevel = '';
  String hwDate = '';
  String vendorCode = '';
  String vendorName = '';
  bool uhfEnabled = false;
  bool hfEnabled = false;
  bool qrEnabled = false;
  bool rs485Enabled = false;
  bool irEnabled = false;
  bool securityEnabled = false;

  // 设备信息字段
  List<int> deviceNameBytes = List.filled(12, 0);
  List<int> macAdd = List.filled(6, 0);
  List<int> hwTime = List.filled(3, 0);
  List<int> swVersionBytes = List.filled(4, 0);
  List<int> swTime = List.filled(3, 0);
  List<int> hwModelByte = List.filled(4, 0);
  List<int> companyCode = List.filled(24, 0);
  List<int> companyName = List.filled(32, 0);
  List<int> hwVersionBytes = List.filled(2, 0);
  List<int> battery = List.filled(4, 0);
  int batCapMaH = 0;

  RfidDeviceInfo();

  void fromRawData({
    required List<int> deviceName,
    required List<int> macAdd,
    required List<int> hwVersion,
    required List<int> hwTime,
    required List<int> swVersion,
    required List<int> swTime,
    required List<int> battery,
    required List<int> hwModelByte,
    required int batCapMaH,
    required List<int> companyCode,
    required List<int> companyName,
  }) {
    deviceNameBytes = deviceName;
    this.macAdd = macAdd;
    hwVersionBytes = hwVersion;
    this.hwTime = hwTime;
    swVersionBytes = swVersion;
    this.swTime = swTime;
    this.battery = battery;
    this.hwModelByte = hwModelByte;
    this.batCapMaH = batCapMaH;
    this.companyCode = companyCode;
    this.companyName = companyName;
    // 解析字符串
    this.deviceName = DataConvert.convertASCII(deviceName);
    macAddress = DataConvert.getHexString(macAdd, 0, 6);
    this.hwVersion = "${DataConvert.bcdStr(hwVersion[0])}.${DataConvert.bcdStr(hwVersion[1])}";
    hwDate = "${DataConvert.bcdStr(hwTime[0])}.${DataConvert.bcdStr(hwTime[1])}.${DataConvert.bcdStr(hwTime[2])}";
    this.swVersion = "${DataConvert.bcdStr(swVersion[0])}.${DataConvert.bcdStr(swVersion[1])}.${DataConvert.bcdStr(swVersion[2])}.${DataConvert.bcdStr(swVersion[3])}";
    batteryLevel = "${DataConvert.bcdStr(battery[0])}${DataConvert.bcdStr(battery[1])}${DataConvert.bcdStr(battery[2])}${DataConvert.bcdStr(battery[3])}mAH";
    vendorCode = DataConvert.getHexString(companyCode, 0, 24);
    vendorName = DataConvert.convertASCII(companyName);
    // 支持模块解析
    qrEnabled = true;
    rs485Enabled = false;
    hfEnabled = false;
    irEnabled = false;
    securityEnabled = false;
    if ((hwModelByte[0] & 0x80) != 0) uhfEnabled = true;
    if ((hwModelByte[1] & 0x80) != 0) rs485Enabled = true;
    if ((hwModelByte[3] & 0x08) != 0) hfEnabled = true;
    if ((hwModelByte[3] & 0x04) != 0) irEnabled = true;
    if ((hwModelByte[3] & 0x02) != 0) irEnabled = true;
    if ((hwModelByte[3] & 0x01) != 0) securityEnabled = true;
  }

  String getInfoString() {
    String de = "";
    if (uhfEnabled) de += "超高频RFID|";
    if (rs485Enabled) de += "RS485抄表|";
    if (hfEnabled) de += "高频RFID|";
    if (irEnabled) de += "激光红外|";
    if (securityEnabled) de += "安全单元|";
    return "设备型号 $deviceName\n"
        "设备ID号 $macAddress\n"
        "硬件版本号 $hwVersion\n"
        "硬件日期 $hwDate\n"
        "软件版本号 $swVersion\n"
        "电池容量 $batteryLevel\n"
        "支持的模块 $de\n"
        "自动关机时间 $batCapMaH分\n"
        "厂商编码 $vendorCode\n"
        "厂商名称 $vendorName\n";
  }
} 


// 新增结构体
class RfidBatteryInfo {
  final int voltage;
  final int quantity;
  RfidBatteryInfo(this.voltage, this.quantity);
}

class RfidTagInfo {
  final String epc;
  final String? tid;
  RfidTagInfo(this.epc, [this.tid]);
}